package com.xlx.ui.controller;

import com.alibaba.fastjson.JSONObject;
import com.xlx.ui.domain.DTO.FriendsDTO;
import com.xlx.ui.domain.MO.Friends;
import com.xlx.ui.domain.VO.ResultBody;
import com.xlx.ui.service.FriendsService;
import com.xlx.ui.service.MessageService;
import com.xlx.ui.service.error.errorImpl.BizException;
import com.xlx.ui.service.error.errorImpl.ResponseCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @author: 张川
 * @date: 2021-04-16 23:59
 */
@RestController
@RequestMapping("/friends")
@Api(tags = "好友管理")
public class FriendsController {

    private FriendsService friendsService;
    private MessageService messageService;


    public FriendsController(FriendsService friendsService, MessageService messageService) {
        this.friendsService = friendsService;
        this.messageService = messageService;
    }

    @ApiOperation(value = "添加好友")
    @PostMapping("")
    public ResultBody add(@RequestBody JSONObject params){
        Integer userId = params.getInteger("userId");
        Integer friendsId = params.getInteger("friendsId");
        boolean add = friendsService.add(userId, friendsId);
        return add?ResultBody.success():ResultBody.error();
    }

    @ApiOperation(value = "查询我的好友")
    @GetMapping("/search/{userId}")
    public ResultBody search(@PathVariable(value = "userId")Integer userId){
        List<FriendsDTO> search = friendsService.search(userId);
        return ResultBody.success(search);
    }

    @ApiOperation(value = "修改备注")
    @PostMapping("/bz")
    public ResultBody update(@RequestBody Friends friends){
        boolean setbz = friendsService.setbz(friends);
        return setbz?ResultBody.success():ResultBody.error();
    }
    @ApiOperation(value = "删除好友")
    @DeleteMapping("/{userId}/{friend}")
    public ResultBody remove(@PathVariable(value = "userId")Integer userId,@PathVariable(value = "friend")Integer friends){
        if(userId == null || friends == null){
           throw new BizException(ResponseCodeEnum.PARAMETER_INVALID);
        }
        Friends friends1 = new Friends();
        friends1.setUserid(userId);
        friends1.setFriendsid(friends);
        Boolean remove = friendsService.remove(friends1);
        Boolean remove1 = messageService.remove(userId,friends);
        return remove&&remove1?ResultBody.success():ResultBody.error();
    }

}
